package com.situ.web.servlet;

import com.situ.web.service.ICourseService;
import com.situ.web.service.impl.CourseServiceImpl;
import com.situ.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/course")
public class CourseServlet extends HttpServlet {
    private ICourseService courseService = new CourseServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        if(method == null ||method.equals("")) {
            method = "selectByPage";
        }
            switch (method){
                case"selectByPage":
                    selectByPage(req,resp);
                    break;
            }




    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if(pageNo == null ||pageNo.equals("")){
            pageNo = "1";
        }
     if(pageSize == null ||pageSize.equals("")){
        pageSize = "5";
        }
        PageInfo pageInfo = courseService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/course_list.jsp").forward(req,resp);
    }
}
